unit display;

interface
uses types, main, forms, Controls, ukapsule,windows,ExtCtrls;

type
  TKDisplayFlags = record
    isFullscreen: boolean;
  end;
  //    loading_time: integer;

// Display --->
type TKDisplay = class
  constructor Create(frm: tklipermainform);
   public
    ignore_click: boolean;
    flags: TKDisplayFlags;
    frm: TKliperMainForm;
    function isFullscreen: boolean;
    procedure fullscreenOn;
    procedure fullscreenOff;
    procedure setPanScan(x_res,y_res:integer);
    procedure syncWithOutput;
    procedure Onmousemove(x,y:integer);
    procedure dubleclick_fullscreen(Sender: TObject);
    private
    procedure showCursor(show_: boolean);
 private

   pan_scan_mode : real ;
   isfullscrren_var : boolean;
   //time_after_last_mouse_move:integer ;
   cursorPos: TPoint;
   cursorIsNotMoved: boolean;
   hidertimer:TTimer;

   procedure ontimer(sender:Tobject);
end;

var kdisplay: TKdisplay;

implementation
uses player,upomocnicza;
{ TKDisplay }

procedure TKDisplay.dubleclick_fullscreen(Sender: TObject);
begin

if isFullscreen then
fullscreenOff else fullscreenOn;
ignore_click:=true;
end;

procedure TKDisplay.Onmousemove(x,y:integer);
var
  cursorpos_now: TPoint;
begin
    if kdisplay.flags.isFullscreen then
begin
    getcursorpos(cursorpos_now);
    begin
    kdisplay.showcursor(true);
    end;
  if  cursorPos_now.Y >(Screen.Height-100) then
    ShowWindow(mainform.playPanel.Handle, SW_SHOW);
 kdisplay.cursorPos:=cursorpos_now
   end;

   end;

procedure TKDisplay.setPanScan(x_res,y_res:integer);
begin
   pan_scan_mode:= y_res/x_res;
   syncWithOutput;
end;


procedure TkDisplay.ontimer(sender:Tobject);
var
  cursorpos_now: TPoint;

begin
  getcursorpos(cursorpos_now);
  if not kdisplay.flags.isFullscreen then exit;
  if (cursorpos_now.y > (screen.height - mainform.playpanel.height)) then exit;
  if ((kdisplay.cursorPos.x = cursorpos_now.x) and (kdisplay.cursorPos.y = cursorpos_now.y)) then
  begin


      ShowWindow(mainform.playPanel.Handle, SW_HIDE);

     showcursor(false);


  end;

  kdisplay.cursorPos := cursorpos_now;
end;



function TKDisplay.isFullscreen: boolean;
begin
  result:=isfullscrren_var;
end;


constructor TKDisplay.Create(frm: tklipermainform);
begin
  self.frm:=frm;
  pan_scan_mode:=1;
  hidertimer:=TTimer.Create(nil);
hidertimer.Interval:=5000;
  hidertimer.OnTimer:= ontimer;
   ignore_click:=false;
end;

procedure TKDisplay.showCursor(show_: boolean);
begin
with mainform do begin
  if show_=true then
  begin
    frm.Cursor := crDefault;
    screen.Cursor := crDefault;
  end else
  begin
    if not screen.Cursor = crNone then
    begin
      frm.Cursor := crNone;
      screen.Cursor := crNone;
    end;
  end;
end;
end;

procedure TKDisplay.syncWithOutput;
var
  aspect: real;
  myaspect: real;
  res: tpoint;
begin
  with mainform do begin
  if not ready then exit;
  if not kdisplay.flags.isFullscreen then
  begin
    kapsule.Width := placefor.Width;
    kapsule.Height := placefor.Height;
  end;
  res.X := kplayer.engine.getfileinfo.VIDEO_WIDTH;
  res.y := kplayer.engine.getfileinfo.VIDEO_HEIGHT;

if not (pan_scan_mode=1) then   //nieiwem dalczego ale jak niema tego ifa to cos pierdoli
 res.y := round(res.X * pan_scan_mode);

 //if we don't know width or height, than just align to formsize
  if ((res.x = 0) or (res.y = 0)) then begin
    screenpanel.width := viewpanel.width;
    screenpanel.height := viewpanel.height;
    screenpanel.Left := 0;
    screenpanel.Top := 0;
    exit;
  end;
  aspect := res.x / res.y;
  myaspect := viewpanel.Width / viewpanel.Height;
  if myaspect < aspect then
  begin
    screenpanel.Width := viewpanel.Width;
    screenpanel.Height := round(viewpanel.Width / aspect);
    screenpanel.Left := 0;
    screenpanel.Top := round((viewPanel.Height) / 2 - screenpanel.Height / 2 - playPanel.Height / 2);
  end else
  begin
    screenpanel.Height := round(viewpanel.Height);
    screenpanel.Width := round(viewpanel.Height * aspect);
    screenpanel.left := round((viewpanel.Width) / 2 - screenpanel.Width / 2);
    screenpanel.Top := 0;
  end;
  end;
end;

procedure TKDisplay.fullscreenOff;
begin
  isfullscrren_var:=false;
  with frm do begin
  ShowWindow(playPanel.Handle, SW_NORMAL);
  mainform.Show;
  windows.SetParent(kapsule.Handle, placefor.Handle);
  kapsule.Width := placefor.Width;
  kapsule.Height := placefor.Height;
  kdisplay.syncWithOutput;
  pomocnicza.hide;
  kdisplay.flags.isFullscreen := false;
  playPanel.Top := mainpanel.Height - playPanel.Height;
  playPanel.Width := mainpanel.Width;
  playPanel.Align := alBottom;

  sublabel.Width := subpanel.Width;
  resizepanel.Show;
  FormResize(nil);
  end;
end;

procedure TKDisplay.fullscreenOn;
begin
  isfullscrren_var:=true;
  with frm do begin
  playPanel.Top := mainpanel.Height - playPanel.Height;
  mainform.Hide;
  kdisplay.flags.isFullscreen := true;
  pomocnicza.Show;
  windows.SetParent(kapsule.Handle, pomocnicza.Handle);
  pomocnicza.WindowState := wsMaximized;
  kapsule.Width := pomocnicza.Width;
  kapsule.Height := pomocnicza.Height;
  kdisplay.syncWithOutput;
  sublabel.Width := subpanel.Width;
  resizepanel.Hide;
  FormResize(nil);
  screenpanel.pozycjonuj;
  end;
end;

// --------------------------------------------------------------
{
procedure Tklipermainform.loadingTimer(Sender: TObject);
var
  i1: Integer;
begin
  application.ProcessMessages;
  caption := busy_caption;
  if loading_time > 4 then loading_time := 0;
  loading_time := loading_time + 1;
  for i1 := 0 to loading_time do

    Caption := Caption + '.';

end;
}



end.
